﻿
@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IPageModelCatche<List<HomeNewsAloneViewModel>>  _pageModelCatche

    <CnGalWebSite.Components.Containers.MiniModeContainer>
    <section class="news-card">
        <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup Title="最新动态" Items="_items.Take(_dataCacheService.IsApp?4:6)" MoreLink="/articles/news" ForceShowMore>
            <div class="group">
                @foreach (var item in context)
                {
                    <MDCard Class="item" ShowHoverStyle="true">
                        <a href="@($"/entries/index/{item.GroupId}")" class="user-image">
                            <img src="@item.Image" alt="@item.Title" loading="lazy" />
                        </a>
                        <a class="content" @onclick="@(()=>OnClickNews(item))" href="@(string.IsNullOrWhiteSpace(item.Link) ? ("/articles/index/" + item.ArticleId) : item.Link)" target="@(string.IsNullOrWhiteSpace(item.Link)?"":"_blank")">
                            <div class="head">
                                <div class="name text-truncate-1">@item.Title</div>
                                <div class="time  label-large">@item.Time.ToTimeFromNowString()</div>
                            </div>
                            <div class="text text-truncate-1 label-large">@item.Text</div>
                        </a>
                    </MDCard>
                }
            </div>
        </CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup>
    </section>
    <CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent @ref="trackEven" />

</CnGalWebSite.Components.Containers.MiniModeContainer>

@code {

    List<IEnumerable<HomeNewsAloneViewModel>> _items { get; set; } = new List<IEnumerable<HomeNewsAloneViewModel>>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent trackEven;


    protected override async Task OnInitializedAsync()
    {

        try
        {
            var items = await _pageModelCatche.GetCache(ToolHelper.WebApiPath + "api/home/GetHomeNewsView");

            var i = 0;
            _items.Clear();
            while (i < items.Count)
            {
                _items.Add(items.Skip(i).Take(3));
                i += 3;
            }


            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取主页动态列表失败");
        }
    }

    public async Task OnClickNews(HomeNewsAloneViewModel model)
    {
        if (trackEven != null)
        {
            await trackEven.SendTrackEvent(TrackEventType.Click, TrackEventDataType.News, model.ArticleId.ToString(), model.Text);
        }
    }
}
